package com.example.springtrans.controller;

import com.example.springtrans.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: hp
 * Date: 2025-01-28
 * Time: 12:50
 */
@RestController
@RequestMapping("/trans")
@Slf4j
public class TransactionalController {
    @Autowired
    private UserService service;

    @Transactional
    @RequestMapping("/r1")
    public String r1(String name,String password) {
        //用户注册
        Integer result = service.insertUserInfo(name,password);
        if(result == 1) {
            log.info("上传成功");
        }
        int a = 10/0;
        return "注册成功";
    }



    @Transactional
    @RequestMapping("/r2")
    public String r2(String name,String password) {
        //用户注册
        Integer result = service.insertUserInfo(name,password);
        if(result == 1) {
            log.info("上传成功");
        }
        try {
            int a = 10/0;
        }catch (Exception e) {
            log.error("发生异常");
        }

        return "注册成功";
    }



    @Transactional
    @RequestMapping("/r3")
    public String r3(String name,String password) {
        //用户注册
        Integer result = service.insertUserInfo(name,password);
        if(result == 1) {
            log.info("上传成功");
        }
        try {
            int a = 10/0;
        }catch (Exception e) {
            log.error("发生异常");
            throw e;
        }

        return "注册成功";
    }


    @Transactional
    @RequestMapping("/r4")
    public String r4(String name,String password) {
        //用户注册
        Integer result = service.insertUserInfo(name,password);
        if(result == 1) {
            log.info("上传成功");
        }
        try {
            int a = 10/0;
        }catch (Exception e) {
            log.error("发生异常");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }

        return "注册成功";
    }


    @Transactional(rollbackFor = Exception.class)
    @RequestMapping("/r5")
    public String r5(String name,String password) throws IOException {
        //用户注册
        Integer result = service.insertUserInfo(name,password);
        if(result == 1) {
            log.info("上传成功");
        }

        if(true) {
            throw new IOException();
        }

        return "注册成功";
    }


    @Transactional(isolation = Isolation.DEFAULT)
    @RequestMapping("/r6")
    public String r6(String name,String password) throws IOException {
        //用户注册
        Integer result = service.insertUserInfo(name,password);
        if(result == 1) {
            log.info("上传成功");
        }

        if(true) {
            throw new IOException();
        }

        return "注册成功";
    }

}
